package com.sunshine.lnuplus.viewModel

import android.app.Application
import android.content.Context
import android.util.TypedValue
import androidx.lifecycle.MutableLiveData
import com.sunshine.lnuplus.base.BaseViewModel
import com.sunshine.lnuplus.model.LessonStyle
import com.sunshine.lnuplus.model.room.Lesson
import com.sunshine.lnuplus.model.room.TimetableRepository
import com.sunshine.lnuplus.utils.DateUtils

class SplashViewModel(application: Application) : BaseViewModel(application){

    val repository: TimetableRepository = TimetableRepository(application)
    private val lessonStyle: LessonStyle
    private val appSetting = application.getSharedPreferences("com.sunshine.lnuplus_preferences",Context.MODE_PRIVATE)
    private val appSettingEdit = appSetting.edit()
    private val dateHeight = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        50f,
        application.resources.displayMetrics
    ).toInt()

    init {
        val sharedPreferences = appSetting
        lessonStyle = LessonStyle(
            weekCount = sharedPreferences.getInt("weeks_count", 30),
            height = sharedPreferences.getInt("height",175),
            textSize = sharedPreferences.getInt("text_size", 10),
            textColor = sharedPreferences.getString("text_color", "#FFFFFF")!!,
            showWeekend = sharedPreferences.getBoolean("show_weekend", true),
            widthDistant = sharedPreferences.getInt("width_distant", 5),
            heightDistant = sharedPreferences.getInt("height_distant", 10),
            count = sharedPreferences.getInt("every_day_count", 10),
            round = sharedPreferences.getInt("radius", 30),
            showTeacher = sharedPreferences.getBoolean("show_teacher", true),
            padding = sharedPreferences.getInt("padding", 5),
            alpha = sharedPreferences.getInt("alpha", 200),
            showTime = sharedPreferences.getBoolean("show_time", true),
            timeTextSize = sharedPreferences.getInt("time_text_size", 10),
            lessonTime = sharedPreferences.getString("lesson_time","8:00\n" +
                    "8:50|9:00\n" +
                    "9:50|10:10\n" +
                    "11:00|11:10\n" +
                    "12:00|13:30\n" +
                    "14:20|14:30\n" +
                    "15:20|15:40\n" +
                    "16:30|16:40\n" +
                    "17:30|18:00\n" +
                    "18:50|19:00\n" +
                    "19:50")!!.split("|") as ArrayList<String>,
            dateColor = sharedPreferences.getString("date_color", "#D81B60")!!,
            dateAndTimeTextColor = sharedPreferences.getString("date_and_time_text_color", "#FFFFFF")!!,
            showNotInThisWeek = sharedPreferences.getBoolean("show_not_in_this_week", true),
            startMonth = sharedPreferences.getInt("start_month", if(DateUtils.month > 8) 8 else 2),
            startDay = sharedPreferences.getInt("start_day", 2),
            detailShowNotInThisWeek = sharedPreferences.getBoolean("detail_show_not_in_this_week", true),
            sundayFirst = sharedPreferences.getBoolean("sunday_first", false),
            calenderMode = sharedPreferences.getBoolean("calender_mode", false),
            textLeft = sharedPreferences.getBoolean("text_left", true),
            nextDay = sharedPreferences.getBoolean("next_day", true),
            nextDayTime = sharedPreferences.getInt("next_day_time", 21)
        )
    }

    fun getAllLesson() : List<Lesson>{
        return repository.getLesson()
    }

    fun getLessonStyle() : MutableLiveData<LessonStyle>{
        val liveData = MutableLiveData<LessonStyle>()
        liveData.value = lessonStyle
        return liveData
    }

    fun isFirstStart(): Boolean {
        return appSetting.getBoolean("first_start", true)
    }

    fun hasStarted(){
        appSettingEdit.putBoolean("first_start", false)
    }

    override fun onCleared() {
        super.onCleared()
        appSettingEdit.apply()
    }

    fun initSetting() {
        appSettingEdit.apply {
            putInt("widget_height", 150)
            putInt("weeks_count", 30)
            putInt("height",175)
            putInt("text_size", 10)
            putString("text_color", "#FFFFFF")
            putBoolean("show_weekend", true)
            putInt("width_distant", 5)
            putInt("height_distant", 10)
            putInt("every_day_count", 10)
            putInt("radius", 30)
            putBoolean("show_teacher", true)
            putInt("padding", 5)
            putInt("alpha", 200)
            putBoolean("show_time", true)
            putInt("time_text_size", 10)
            putString("lesson_time","8:00\n8:50|9:00\n9:50|10:10\n11:00|11:10\n12:00|13:30\n14:20|14:30\n15:20|15:40\n16:30|16:40\n17:30|18:00\n18:50|19:00\n19:50")
            putString("date_color", "#D81B60")
            putInt("time_text_size", 10)
            putString("date_and_time_text_color", "#FFFFFF")
            putBoolean("show_not_in_this_week", true)
            putBoolean("detail_show_not_in_this_week", true)
            putBoolean("sunday_first", false)
            putBoolean("calender_mode", false)
            putBoolean("next_day", false)
            putInt("next_day_time", 21)
        }
    }

    fun getBackgroundMohu(): Int {
        return appSetting.getInt("background_mohu", 0)
    }

    fun getInitLessonList(): String {
        return appSetting.getString("init_lesson_list", "")!!
    }
}